package com.akn.mvvmlibrary.viewmodel

import android.app.Activity
import android.graphics.Typeface
import android.view.Gravity
import androidx.core.content.ContextCompat
import androidx.lifecycle.ViewModel
import com.akn.mvvmlibrary.R
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.SizeUtils
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.widget.tab.QMUIBasicTabSegment
import com.qmuiteam.qmui.widget.tab.QMUITab

open class BaseViewModel : ViewModel() {
    /*标题名称*/
    open fun tittleString(): String {
        return ""
    }

    /*是否显示返回键*/
    open fun showBack(): Boolean {
        return true
    }

    /**是否隐藏返回按钮右边文字*/
    open fun isShowBackText(): Boolean {
        return false
    }

    /*是否显示标题右边文字按钮*/
    open fun showRightText(): Boolean {
        return false
    }

    /*是否显示标题右边图片按钮*/
    open fun showRightImage(): Boolean {
        return false
    }

    /*设置标题栏右边按钮的资源图片*/
    open fun imageRightResId(): Int {
        return R.mipmap.shezhi
    }

    /*标题栏右键点击事件,图片和文字共一个点击事件*/
    open fun rightImageAction() {

    }

    /*设置右边文字按钮文本*/
    open fun rightText(): String {
        return ""
    }

    /**右边文字按钮颜色*/
    open fun rightTextColor(): Int {
        return buildResColor(R.color.mainColor)
    }

    /**返回按钮颜色包含返回文字*/
    open fun backTintColor(): Int {
        return buildResColor(R.color.mainColor)
    }


    open fun buildResColor(colorId: Int): Int {
        return ContextCompat.getColor(getActivity(), colorId)
    }

    /*显示标题栏底部分割线
    * 默认不展示*/
    open fun showViewLine(): Boolean {
        return false
    }

    /*返回键监听*/
    open fun backAction() {
        getActivity().finish()
    }

    open fun getActivity(): Activity {
        return ActivityUtils.getTopActivity()
    }

    /**
     * 构建QMUITab
     */
    open fun buildQMUITab(
        text: String,
        resId: Int = 0,
        signCount: Int = 0,
        normalColor: Int = R.color.qmui_config_color_gray_5,
        selectColor: Int = R.color.mainColor,
        gravity: Int = Gravity.TOP
    ): QMUITab {
        val basicTabSegment = QMUIBasicTabSegment(getActivity())
        val tabBuilder = basicTabSegment.tabBuilder()
        if (resId != 0) {
            val iconShowSize = QMUIDisplayHelper.dp2px(getActivity(), 20)
            val normalDrawable = ContextCompat.getDrawable(getActivity(), resId)
            val selectDrawable = ContextCompat.getDrawable(getActivity(), resId)
            tabBuilder.setNormalDrawable(normalDrawable)
                .setSelectedDrawable(selectDrawable)
                .setNormalIconSizeInfo(iconShowSize, iconShowSize)
        }
        return tabBuilder.setText(text)
            .setSignCount(signCount)
            .setNormalColor(ContextCompat.getColor(getActivity(), normalColor))
            .setSelectColor(ContextCompat.getColor(getActivity(), selectColor))
            .setTypeface(Typeface.DEFAULT, Typeface.DEFAULT_BOLD)
            .setGravity(gravity)
            .build(getActivity())
    }

}